package com.lyubia.shop.portal.controller;

import com.lyubia.shop.api.pojo.Admin;
import com.lyubia.shop.api.pojo.CartVo;
import com.lyubia.shop.api.service.CartService;
import com.lyubia.shop.api.vo.CartResult;
import com.lyubia.shop.common.result.BaseResult;
import com.lyubia.shop.common.utils.CookieUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Reference(version = "1.0")
    private CartService cartService;

    @GetMapping("/index")
    public String index(Model model, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("user");
        CartResult cartListInfo = cartService.getCartListInfo(Integer.parseInt(admin.getAdminId().toString()));
        model.addAttribute("cartResult",cartListInfo);
        return "cart";
    }

    @PostMapping("/saveCartVo")
    @ResponseBody
    public BaseResult saveCart(CartVo cartVo, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("user");
        return cartService.saveCartVo(cartVo, Integer.parseInt(admin.getAdminId().toString()));
    }

    @PostMapping("/getCartNum")
    @ResponseBody
    public Integer getCartNum(HttpSession session) {
        Admin admin = (Admin) session.getAttribute("user");
        return cartService.getCartNum(Integer.parseInt(admin.getAdminId().toString()));
    }

    @PostMapping("/clearCart")
    @ResponseBody
    public BaseResult clearCart(HttpSession session) {
        Admin admin = (Admin) session.getAttribute("user");
        return cartService.clearCart(Integer.parseInt(admin.getAdminId().toString()));
    }

    @PostMapping("/delCartVo")
    @ResponseBody
    public BaseResult delCartVo(HttpSession session, Integer goodsId) {
        Admin admin = (Admin) session.getAttribute("user");
        return cartService.delCartVo(Integer.parseInt(admin.getAdminId().toString()), goodsId);
    }



}
